#coding: utf8
import re
from pygeocoder import Geocoder
import time
from datetime import datetime
import urlparse
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview
from decimal import *

class AtrapaloUtils(object):

    utils = None

    def fillRestaurant(self,response,hxs,restaurant):
        try:
                           
            restaurant['restaurant_name'] = self.__getRestName(self.utils.convertListToString(hxs.select('//h1[@class="item fn"]/text()').extract()))
            address = hxs.select('(//div[@class="pad10 fontSmall5 lineHeight15 grey8"]/ul[@class="padbot10"]/li[@class="marginbot5"])[1]/text()').extract()
            address = self.utils.convertListToString(address)
            address = self.utils.cleanString(address)

            restaurant['city']='NA' 
            restaurant['address'] = address
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(address)
            restaurant['score']=self.__getScore(self.utils.convertListToString(hxs.select('//div[@id="div_0"]/div[@class="cajanegra_izq floatl block"]/div[@class="opi_general_rate"]/span[@class="userrating"]/text()').extract()))


            """list of types of restaurant"""
            restaurant['rest_type']=self.__getRestType(hxs.select('//span/text()[contains(.,"Cocina:")]/../..'))
            
            rev = self.__getReviews(self.utils.convertListToString(hxs.select('//div[@id="div_0"]/div[@class="cajanegra_izq floatl block"]/div[@class="opi_general_rate"]/span[@class="description "]/span[@class="opinions"]/strong/text()').extract()))
            if rev is None:
                rev = self.__getReviews(self.utils.convertListToString(hxs.select('//div[@id="div_0"]/div[@class="cajanegra_izq floatl block"]/div[@class="opi_general_rate"]/span[@class="description padtop8"]/span[@class="opinions"]/strong/text()').extract()))
            restaurant['reviews_total']=rev

            restaurant['reviews'] = [] 

            sComida=None
            sServicio=None
            sAtmosfera=None
            sCalidad=None
            for r in hxs.select('//ul[@class="opinion_criterios_0 opinion_criterios"]/li'):
                
                tmp = r.select('span[@class="opi_categoria floatl"]/text()').extract()
                tmp_s = self.utils.convertListToString(tmp).strip().lower()
                if tmp_s == "comida":
                    sComida = self.__getScore(self.utils.convertListToString(r.select('span[@class="opi_valor"]/text()').extract()))
                    continue
                if tmp_s == "servicio":
                    sServicio = self.__getScore(self.utils.convertListToString(r.select('span[@class="opi_valor"]/text()').extract()))
                    continue
                if tmp_s == "ambiente":
                    sAtmosfera = self.__getScore(self.utils.convertListToString(r.select('span[@class="opi_valor"]/text()').extract()))
                    continue
                if tmp_s == "calidad / precio":
                    sCalidad = self.__getScore(self.utils.convertListToString(r.select('span[@class="opi_valor"]/text()').extract()))
                    continue

            restaurant['score_comida']=sComida
            restaurant['score_servicio']=sServicio
            restaurant['score_atmosfera']=sAtmosfera
            restaurant['score_calidadP']=sCalidad

            restaurant['url_rest'] = None
            restaurant['cp'] = None

            self.utils.log("DEBUG","Restaurant: " + str(restaurant),self.utils.DEBUG)
        
            return

        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)


    def fillComments(self,response,hxs,restaurant):
        try:
            #from scrapy.shell import inspect_response
            #inspect_response(response)

            for l in hxs.select('//div[@class="userOpina hreview "]'):
                restaurantReview = RestaurantReview()
                restaurantReview['author']= l.select('div[@class="colValora"]/p[@class="user"]/span/text()').extract()
                restaurantReview['date']= self.__getCommentDate(self.utils.convertListToString(l.select('(div[@class="colValora"]/ul[@class="infUser"]/li)[1]/strong/text()').extract()))
                restaurantReview['score']=self.__getScore(self.utils.convertListToString(l.select('div[@class="colPuntua"]/p[@class="valor rating"]/span[@class="num"]/text()').extract()))
                
                restaurantReview['comment_positive']=self.utils.convertListToString(l.select('div[@class="colValora"]/div/@id[contains(.,"review_")]/../p[@class="positivo"]/span/span[@class="text1"]/text()').extract())               
                restaurantReview['comment_negative']=self.utils.convertListToString(l.select('div[@class="colValora"]/div/@id[contains(.,"review_")]/../p[@class="negativo"]/span/span[@class="text2"]/text()').extract())
               
                restaurantReview['score_calidadP']=self.__getScore(self.utils.convertListToString(l.select('((div[@class="colValora"]/div/@id[contains(.,"aspects")]/../div)[1]/ul/li)[1]/span/text()').extract()))
                restaurantReview['score_comida']=self.__getScore(self.utils.convertListToString(l.select('((div[@class="colValora"]/div/@id[contains(.,"aspects")]/../div)[1]/ul/li)[2]/span/text()').extract()))
                restaurantReview['score_servicio']=self.__getScore(self.utils.convertListToString(l.select('((div[@class="colValora"]/div/@id[contains(.,"aspects")]/../div)[1]/ul/li)[3]/span/text()').extract()))
                restaurantReview['score_atmosfera']=self.__getScore(self.utils.convertListToString(l.select('((div[@class="colValora"]/div/@id[contains(.,"aspects")]/../div)[1]/ul/li)[4]/span/text()').extract()))
                restaurant['reviews'].append(restaurantReview)
                

            return

        except Exception,e:
            self.utils.logProcessDetail("Error in fillComments: " + str(e),self.utils.ERROR)


    def __getCommentDate(self,inDate):
        try:
            dDate=None
            tmp_s=None
            if inDate is not None and len(inDate)>0:
                dDate = datetime.strptime(inDate, '%d/%m/%Y').date()
                tmp_s = dDate.strftime('%d/%m/%Y')
     
            return tmp_s
    
        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e) + "date: " + str(inDate),self.utils.ERROR)



    def __getRestType(self,hxs):
        try:
            d=dict()
            for rest_type in hxs.select('a/text()').extract():
                d[self.utils.convertListToString(rest_type).strip()]=0
            return d.keys()      

        except Exception,e:
            self.utils.logProcessDetail("Error in __getRestType: " + str(e),self.utils.ERROR)


    def __getRestName(self,txtName):
        txtName = txtName.replace("Restaurante","")
        return txtName.strip()
    
    def __getReviews(self,txtReviews):
        tmp_s=""
        try:
            tmp_s = str(txtReviews).lower().strip()
            if len(tmp_s)>0:
                tmp_s = tmp_s.replace("opiniones","")
                tmp_s = tmp_s.replace("opinión","")
                tmp_s = str(int(tmp_s.strip()))
            else:
                tmp_s=None
            return tmp_s
        except Exception,e:
            self.utils.logProcessDetail("Error in __getReviews, data_: " + str(txtReviews) + "Error: " + str(e),self.utils.ERROR)
        
            
    
    def __getScore(self,txtScore):
        tmp_s=""
        try:
            tmp_s = str(txtScore).strip()
            if len(tmp_s)>0:
                tmp_s = str(txtScore).replace(",",".")
                tmp_s = str(Decimal(tmp_s))
            else:
                tmp_s=None
            return tmp_s
        except Exception,e:
            self.utils.logProcessDetail("Error in __getScore, data_: " + str(txtScore) + "Error: " + str(e),self.utils.ERROR)
           


        